/*
 * @Description: 描述信息
 * @Version: 版本号
 * @Author: xuebao
 * @Date: 2023-12-08 10:59:36
 * @LastEditors: renmengxue
 * @LastEditTime: 2023-12-08 14:12:47
 */
import { defineStore } from 'pinia'// 用它定义一个store
import { ColumnProps, ResponseType } from '../store'
import { ref, computed } from 'vue'
import axios from 'axios'
interface TestProps{
  data:ColumnProps[], total:number
}
// 最好是用use开头
export const useTestStore = defineStore('test', {
  // id 是test
  state: ():TestProps => {
    return {
      data: [],
      total: 0
    }
  },
  getters: {
    // 首先推荐箭头函数 state 是定义的数据
    doubleTotal: (state) => {
      return state.total * 2
    },
    // 常规函数
    doublePlus():number {
      // 可以使用this 访问state里面的和getters里面定义的方法
      return this.doubleTotal + 1
    },
    // 接收传递过来的参数
    getDataById: (state) => {
      return (id:string) => state.data.find((column) => column._id === id)
    }
  },
  actions: {
    increaseTotal () {
       this.total++
    },
   async  fetchColumns(params:any = {}) {
    const { currentPage = 1, pageSize = 3 } = params
    const { data } = await axios.get<ResponseType <{count:number, list:ColumnProps[]}>>(`/columns?currentPage=${currentPage}&pageSize=${pageSize}`)
     const { count, list } = data.data

     this.$patch({
      total: count,
      data: list
     })
     return list
  }
  }
})

export const useTest2Store = defineStore('test2', () => {
  const data = ref<ColumnProps[]>([])
  const total = ref(0)

  const doubleTotal = computed(() => total.value * 2)

  const fetchColumns = async (params:any = {}) => {
    const { currentPage = 1, pageSize = 3 } = params
    const { data: rawData } = await axios.get<ResponseType <{count:number, list:ColumnProps[]}>>(`/columns?currentPage=${currentPage}&pageSize=${pageSize}`)
     const { count, list } = rawData.data

     data.value = list
    total.value = count
    return list
  }
  return {
    total,
    fetchColumns,
    data
  }
})
